package com.ruoyi.project.modules.pageTag.controller;

import java.util.List;

import com.ruoyi.project.modules.filePageTagRef.domain.FilePageTagRef;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.pageTag.domain.PageTag;
import com.ruoyi.project.modules.pageTag.service.IPageTagService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 页面标签信息Controller
 *
 * @author smallrain
 * @date 2022-03-22
 */
@Controller
@RequestMapping("/pageTag/pageTag")
public class PageTagController extends BaseController {
    private String prefix = "pageTag/pageTag";

    @Autowired
    private IPageTagService pageTagService;

    @RequiresPermissions("pageTag:pageTag:view")
    @GetMapping()
    public String pageTag() {
        return prefix + "/pageTag";
    }

    /**
     * 查询页面标签信息列表
     */
    //@RequiresPermissions("pageTag:pageTag:list")
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(Long pageId) {
        PageTag pageTag = new PageTag();
        pageTag.setPageId(pageId);
        List<PageTag> list = pageTagService.selectPageTagList(pageTag);
        return AjaxResult.success(list);
    }

    /**
     * 导出页面标签信息列表
     */
    @RequiresPermissions("pageTag:pageTag:export")
    @Log(title = "页面标签信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PageTag pageTag) {
        List<PageTag> list = pageTagService.selectPageTagList(pageTag);
        ExcelUtil<PageTag> util = new ExcelUtil<PageTag>(PageTag.class);
        return util.exportExcel(list, "页面标签信息数据");
    }

    /**
     * 新增页面标签信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存页面标签信息
     */
    //@RequiresPermissions("pageTag:pageTag:add")
    @Log(title = "页面标签信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PageTag pageTag) {
        return toAjax(pageTagService.insertPageTag(pageTag));
    }

    /**
     * 修改页面标签信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PageTag pageTag = pageTagService.selectPageTagById(id);
        mmap.put("pageTag", pageTag);
        return prefix + "/edit";
    }

    /**
     * 修改保存页面标签信息
     */
    //@RequiresPermissions("pageTag:pageTag:edit")
    @Log(title = "页面标签信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PageTag pageTag)
    {
        return toAjax(pageTagService.updatePageTag(pageTag));
    }

    /**
     * 批量修改保存页面标签信息
     */
    //@RequiresPermissions("pageTag:pageTag:edit")
    @Log(title = "批量修改页面标签信息", businessType = BusinessType.UPDATE)
    @PostMapping("/listEdit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody List<PageTag> pageTagList)
    {
        pageTagList.forEach(pageTag -> toAjax(pageTagService.updatePageTag(pageTag)));
        return success();
    }

    /**
     * 删除页面标签信息
     */
    @RequiresPermissions("pageTag:pageTag:remove")
    @Log(title = "页面标签信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(pageTagService.deletePageTagByIds(ids));
    }

    /**
     * 删除页面标签信息
     * @author chenxiaoyu
     * @date 2022/3/22 下午10:19
     * @param id
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/removeById")
    @ResponseBody
    public AjaxResult removeById(Long id) {
        return toAjax(pageTagService.deletePageTagByIds(String.valueOf(id)));
    }

    /**
     * 根据图片id获取 页面标签
     * @author chenxiaoyu
     * @date 2022/3/22 下午8:42
     * @param imgId
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryPageTagByImgId")
    @ResponseBody
    public AjaxResult queryPageTagByImgId(Long imgId) {
        return AjaxResult.success(this.pageTagService.queryPageTagByImgId(imgId));
    }

    /**
     * 根据页面标签id获取页面图片
     * @author chenxiaoyu
     * @date 2022/3/22 下午8:42
     * @param pageTagId
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @GetMapping("/queryFileInfoByPageTagId")
    @ResponseBody
    public AjaxResult queryFileInfoByPageTagId(Long pageTagId) {
        return AjaxResult.success(this.pageTagService.queryFileInfoByPageId(pageTagId));
    }

    /**
     * 保存图片与页面标签关系
     * @author chenxiaoyu
     * @date 2022/3/22 下午10:17
     * @param filePageTagRef
     * @return com.ruoyi.common.core.domain.AjaxResult
     */
    @PostMapping("/saveFilePageTagRef")
    @ResponseBody
    public AjaxResult saveFilePageTagRef(@RequestBody FilePageTagRef filePageTagRef) {
        return toAjax(pageTagService.saveFilePageTagRef(filePageTagRef));
    }
}
